<template>
	<view class="mine-page page">
		<CustomNav title="一键报灾" :isBack="true"></CustomNav>
		<view class="tips-block">
			<view class="iconfont icon-a-tishi2">
				<text class="tips-text">
					提示：请如实填写信息，若因填写信息不真实导致其他问题的，后果将由您自行承担。
				</text>
			</view>
		</view>
		<view class="info-block">
			<view class="info-list">
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">村组</view>
					</view>
					<view class="form-content">
						<uni-data-picker :localdata="township" popup-title="请选择村组" @change="onchange"
							@nodeclick="onnodeclick" class="form-crl1"></uni-data-picker>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">类型</view>
					</view>
					<view class="form-content">
						<picker @change="bindTypeChange" range-key="name" :value="industryIndex" :range="typeList"
							class="form-crl1">
							{{typeIndex==-1 ? '请选择灾害类型':typeList[typeIndex].name}}
						</picker>
						<view class="right">
							<view class="iconfont icon-gengduo"></view>
						</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">位置</view>
					</view>
					<view class="form-content" @click="chooseLocation">
						<input v-model="form.position" :disabled="true" class="form-crl1"
							placeholder-style="color:#000;" placeholder-class="input-placeholder"
							placeholder="请选择灾害位置" />
						<view class="right">
							<view class="iconfont icon-gengduo"></view>
						</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">具体位置</view>
					</view>
					<view class="form-content">
						<input placeholder="请输入具体位置..." v-model="form.specificPosition"
							placeholder-class="input-placeholder" placeholder-style="color:#000;" style="width: 100%;"
							maxlength="150" />
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">灾害情况</view>
					</view>
					<view class="form-content">
						<textarea placeholder="请简述灾害情况..." v-model="form.disasterSituation" style="height: 200rpx;"
							placeholder-class="input-placeholder" placeholder-style="color:#000;" maxlength="300" />
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">发生时刻</view>
					</view>
					<view class="form-content">
						<uni-datetime-picker type="datetime" v-model="form.occurTime" @change="change" class="form-crl"
							:end="endDate" />
						<view class="right">
							<view class="iconfont icon-gengduo"></view>
						</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">上传图片/视频({{form.pic.length+form.video.length}}/9)</view>
					</view>
					<view class="upload">
						<view v-for="(item,index) in form.pic" class="image-item">
							<image mode="aspectFit" :src="item" @click="previewImg(item)" class="img">
							</image>
							<view @click="closeImg(index,1)" style="display: inline;">
								<view class="iconfont">
									<image src="/static/image/delete.pn" class="delete"></image>
								</view>
							</view>
						</view>
						<view v-for="(item,index) in form.video" class="image-item">
							<video id="myVideo" class="img" style="width: 190rpx;height: 200rpx;border-radius: 20rpx;"
								:src="item" @error="videoErrorCallback" show-fullscreen-btn></video>
							<view @click="closeImg(index,2)" style="display: inline;">
								<view class="iconfont">
									<image src="/static/image/delete.png" class="delete"></image>
								</view>
							</view>
						</view>
						<view class="upload-content" v-if="(form.pic.length+form.video.length)<9" @click="chooseMedia">
							<image src="/static/image/upload.png" class="upload"></image>
						</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">是否匿名</view>
					</view>
					<view class="form-content">
						<picker @change="bindAnoChange" range-key="name" :value="anonymousIndex" :range="anonymousList"
							class="form-crl1">
							{{anonymousIndex==-1 ? '请选择是否匿名':anonymousList[anonymousIndex].name}}
						</picker>
						<view class="right">
							<view class="iconfont icon-gengduo"></view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="btn-item">
			<view class="btn">
				<view class="btn normal" @click="submit"> 提交</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		validate
	} from '@/util/validate.js'
	import {
		getCurrentTime
	} from '@/util/util.js'
	import {
		getUserInfo
	} from '@/util/user.js'
	import {
		addAndUpdateDisaster,
		getDisasterType,
		getselectTreeApi
	} from '@/api/report.js'
	import config from '@/config.js';
	import {
		getStorage,
		showToast
	} from '@/util/auth';
	export default {
		data() {
			return {
				form: {
					type: 0,
					position: '',
					specificPosition: '',
					disasterSituation: '',
					occurTime: getCurrentTime(),
					isAnonymous: 0,
					pic: [],
					video: [],
					multiMedia: '',
					latitude: '',
					longitude: '',
					area:[]
				},
				rules: {
					area: {
						name: '村组',
						required: true,
						validator: function(value, form, callback) {
							if (value == 0) {
								callback('请选择村组')
							}
							callback();
						}
					},
					type: {
						name: '灾害类型',
						required: true,
						validator: function(value, form, callback) {
							if (value == 0) {
								callback('请选择灾害类型')
							}
							callback();
						}
					},
					position: {
						name: '位置',
						required: true,
						validator: function(value, form, callback) {
							if (value == 0) {
								callback('请选择位置')
							}
							callback();
						}
					},

					specificPosition: {
						name: '具体位置',
						required: true
					},
					disasterSituation: {
						name: '灾害情况',
						required: true
					},
					occurTime: {
						name: '发生时刻',
						required: true,
						validator: function(value, form, callback) {
							if (value == 0) {
								callback('请选择发生时刻')
							}
							callback();
						}
					},
					multiMedia: {
						name: '图片或视频',
						required: true,
						validator: function(value, form, callback) {
							if (value == 0) {
								callback('请上传图片或视频')
							}
							callback();
						}
					},
					isAnonymous: {
						name: '是否匿名',
						required: true,
						validator: function(value, form, callback) {
							if (value == 0) {
								callback('请选择是否匿名')
							}
							callback();
						}
					},
				},

				typeIndex: -1,
				typeList: [],

				anonymousIndex: -1,
				anonymousList: [{
						id: 1,
						name: '是'
					},
					{
						id: 2,
						name: '否'
					}
				],
				endDate: '',
				township: [],
				villageId: []
			}
		},

		onLoad() {

		},
		methods: {
			onchange(e) {
				const value = e.detail.value
				this.form.area = value
			},
			onnodeclick(node) {},
			getselectTree() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getselectTreeApi({}).then(res => {
					uni.hideLoading()
					const data = res
					data.forEach((item, index) => {
						item.value = item.id
						item.text = item.name
						delete(item.id)
						delete(item.name)
						item.children.forEach((item1, index1) => {
							item1.value = item1.id
							item1.text = item1.name
							delete(item1.id)
							delete(item1.name)
						})
					})
					this.township = data;
				}).catch(() => {
					uni.hideLoading()
				})
			},
			bindTypeChange(e) {
				this.typeIndex = e.detail.value
				this.form.type = this.typeList[this.typeIndex].id
			},
			bindAnoChange(e) {
				this.anonymousIndex = e.detail.value
				this.form.isAnonymous = this.anonymousList[this.anonymousIndex].id
			},
			change(e) {
				this.form.occurTime = e
			},
			getTypeList() {
				getDisasterType().then(res => {
					this.typeList = res
				}).finally(() => {
					uni.stopPullDownRefresh();
				})
			},
			//选择位置
			chooseLocation() {
				uni.chooseLocation({
					latitude: this.$store.getters.position.latitude,
					longitude: this.$store.getters.position.longitude,
					success: (e) => {
						this.form.position = e.address;
						this.form.latitude = e.latitude;
						this.form.longitude = e.longitude;
					},
				})
			},
			chooseMedia(e) {
				let token = getStorage('token');
				uni.chooseMedia({
					maxDuration: 60,
					count: 1,
					mediaType: ['image', 'video'],
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						if (res.length < 2) { // 小于2则没有选择图片
							uni.hideLoading()
							return
						}
						let filePath = res.tempFiles[0].tempFilePath;
						let fileType = res.tempFiles[0].type;
						let that1 = this;
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						uni.uploadFile({
							url: config.requestUrl + '/sbbz/file/uploadApi', //post请求的地址
							filePath: filePath,
							name: 'file',
							fileType: fileType,
							header: {
								'authori-zation': token
							},
							success: (myres) => {
								uni.hideLoading()
								const data = JSON.parse(myres.data);
								if (data.code == 200) {
									if (data.data.fileType == 1) {
										this.form.pic.push(data.data.url)
									} else if (data.data.fileType == 2) {
										this.form.video.push(data.data.url)
									}
								} else {
									uni.hideLoading()
									showToast('上传失败');
								}
							}
						})
						// })
					}
				})
			},
			// 删除图片
			closeImg(index, e) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除当前文件',
					success: function(res) {
						if (res.confirm) {
							if (e == 1) {
								that.form.pic.splice(index, 1);
							} else if (e == 2) {
								that.form.video.splice(index, 1);
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			submit() {
				let form = JSON.parse(JSON.stringify(this.form));
				if (form.pic.length > 0 && form.video.length > 0) {
					form.multiMedia = form.pic.join(',') + ',' + form.video.join(',')
				} else if (form.pic.length > 0 && form.video.length == 0) {
					form.multiMedia = form.pic.join(',')
				} else {
					form.multiMedia = form.video.join(',')
				}
				if (validate(form, this.rules)) {
					addAndUpdateDisaster(form).then(res => {
						uni.showToast({
							title: '提交成功',
							icon: 'none',
							duration: 1000,
							mask: true,
							success: function() {
								setTimeout(function() {
									uni.navigateBack();
								}, 1000)
							},
						})
					}).finally(() => {
						uni.stopPullDownRefresh();
					})
				}
			}
		},
		onShow() {
			this.endDate = getCurrentTime()
			this.getTypeList()
			this.getselectTree()
		}
	}
</script>

<style lang="scss">
	.uni-date-x {
		color: #000 !important;
	}

	.uni-date-x--border {
		border: none !important;
	}

	.uni-icons {
		display: none !important;
	}

	.uni-date__x-input {
		font-size: 32rpx !important;
	}

	.mine-page {
		overflow: hidden;

		.tips-block {
			background-color: #D6E0FF;
			color: #0256FF;
			width: calc(100% - 60rpx);
			display: flex;
			margin-top: 26%;
			margin-left: 30rpx;
			border-radius: 20rpx;
			padding: 30rpx;

			.iconfont {
				line-height: 60rpx;

				.tips-text {
					margin-left: 20rpx;
				}
			}
		}

		.info-block {
			padding: 30rpx;
			background-color: #F7F6F9;

			.info-list {
				background-color: #fff;
				border-radius: 20rpx;
				padding: 30rpx;
				margin-bottom: 20rpx;

				.form-item:last-child {
					border-bottom: none;
				}

				.form-item {
					display: flex;
					flex-direction: column;
					border-bottom: 1px solid #F7F7F7;
					padding-bottom: 30rpx;
					margin-top: 30rpx;

					.name-item {
						display: flex;
						align-items: center;
						margin-bottom: 30rpx;

						.must {
							color: #EA0000;
						}

						.name {
							font-size: 30rpx;
						}
					}

					.form-content {
						display: flex;
						align-items: center;
						justify-content: space-between;
						width: 100%;


						.city {
							position: absolute;
							z-index: 3;
							padding-top: 120rpx;
							padding-left: 40rpx;
							color: #FFFFFF;
							display: flex;
							align-items: center;
							font-size: 30rpx;
						}

						.form-crl1 {
							width: calc(100% - 80rpx);
							font-size: 30rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}

						.form-crl {
							width: 100%;
							font-size: 30rpx;
						}

						.right {
							display: flex;
							align-items: center;

							.iconfont {
								color: #CDCDCD;
								font-size: 32rpx;
							}
						}
					}

					.upload {
						display: flex;
						flex-direction: row;
						flex-wrap: wrap;

						.image-item {
							margin-top: 20rpx;
							margin-right: 20rpx;
							display: flex;
							flex-direction: row;
							// box-sizing: border-box;
							// border: 1px solid #efefef;

							.img {
								width: 190rpx;
								height: 200rpx;
								border-radius: 20rpx;
							}

							.iconfont {
								.delete {
									width: 20px;
									height: 20px;
									position: absolute;
									margin-left: -30rpx;
								}
							}
						}

						.image-item:nth-child(3n) {
							margin-right: 0;
						}

						.delete-item {
							width: 100%;
							display: flex;
							justify-content: flex-end;

							.delete {
								width: 20px;
								height: 20px;
								position: absolute;
							}
						}

						.image-item1 {
							display: flex;
							flex-direction: row;

							.delete {
								margin-top: 10rpx;
								margin-left: -40rpx;
								width: 20px;
								height: 20px;
								position: absolute;
							}
						}

						.upload-content {
							position: relative;
							background: #fafafa;
							border-radius: 5px;
							width: 200rpx;
							height: 200rpx;
							display: flex;
							align-items: center;
							justify-content: center;
							margin-top: 20rpx;

							.title {
								position: absolute;
								top: 50%;
								left: 50%;
							}

							.upload {
								width: 90rpx;
								height: 90rpx;
							}
						}
					}
				}
			}
		}

		.btn-item {
			display: flex;
			justify-content: center;
			margin-bottom: 40rpx;
		}
	}
</style>